package com.greateamd.timetableasy.client.widgets.studyperiod;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.greateamd.timetableasy.client.domain.EventType;
import com.greateamd.timetableasy.client.widgets.EntityContainer;
import com.greateamd.timetableasy.client.widgets.Grid;
import com.greateamd.timetableasy.domain.Course;
import com.greateamd.timetableasy.domain.Event;
import com.greateamd.timetableasy.domain.StudyPeriod;

public class StudyPeriodBuilder extends Composite {

	@UiTemplate("StudyPeriodBuilder.ui.xml")
	interface ListRoomBuilderUIBinder extends UiBinder<Widget, StudyPeriodBuilder> {}
	private static ListRoomBuilderUIBinder uiBinder = GWT.create(ListRoomBuilderUIBinder.class);
	
	private List<StudyPeriod> listOfStudies = new ArrayList<StudyPeriod>();
	
//	@UiField TextBox studyNameTb;
	//----------Studies
	@UiField EntityContainer<StudyPeriod> studiesLb;
	@UiField Button addSpBt;
	@UiField Button removeSpBt;
	//Popup
	private DialogBox dialogBoxSp;
	private TextBox nameSpTb;
	private StudyPeriodTypeListBox typeSpTb;
	private Button popupSpSubmitBt;
	private Button popupSpCloseBt;
	
	//----------Courses
	@UiField EntityContainer<Course> coursesLb;
	@UiField Button addCourseBt;
	@UiField Button removeCourseBt;
	//Popup
	private DialogBox dialogBoxCourse;
	private DateBox dbStart;
	private DateBox dbEnd;
	private TextBox nameTb;
	private TextBox typeTb;
	private Button popupCourseSubmitBt;
	private Button popupCourseCloseBt;
	
	//----------Events
	@UiField EntityContainer<Event> eventsLb;
	@UiField Button addEventBt;
	@UiField Button removeEventBt;
	//Popup
	private DialogBox dialogBoxEvent;
	private TextBox durationEventTb;
	private TextBox labelEventTb;
	private Button popupEventSubmitBt;
	private Button popupEventCloseBt;
	
	public StudyPeriodBuilder() {
		initWidget(uiBinder.createAndBindUi(this));
		popupCourseCloseBt = new Button("Close", popupCourseCloseHandler);
		popupCourseSubmitBt = new Button("Add course", popupCourseSubmitHandler);
		popupSpCloseBt = new Button("Close", popupSpCloseHandler);
		popupSpSubmitBt = new Button("Add study period", popupSpSubmitHandler);
		popupEventCloseBt = new Button("Close", popupEventCloseHandler);
		popupEventSubmitBt = new Button("Add event", popupEventSubmitHandler);
	}
	
	//---------------------------------------------------------------------
	//-------------------------- Handler
	@UiHandler("studiesLb")
	void onStudyChange(ChangeEvent e){
		if(studiesLb.getSelectedEntity() != null){
			coursesLb.clear();
			for(Course course : studiesLb.getSelectedEntity().getCourses()){
				coursesLb.addEntity(course);
			}
		}
	}
	@UiHandler("coursesLb")
	void onCourseChange(ChangeEvent e){
		if(coursesLb.getSelectedEntity() != null){
			eventsLb.clear();
			for(Event event : coursesLb.getSelectedEntity().getEvents()){
				eventsLb.addEntity(event);
			}
		}
	}
	
	@UiHandler("addCourseBt")
	void onAddCourseBtClick(ClickEvent e){
		if(studiesLb.getSelectedIndex() != -1){
			dialogBoxCourse = new DialogBox(false,true);
			dialogBoxCourse.setGlassEnabled(true);
			dialogBoxCourse.setAnimationEnabled(true);
			dialogBoxCourse.setText("Add a course");
			dialogBoxCourse.setWidget(buildAddCourseForm());
			dialogBoxCourse.center();
			dialogBoxCourse.show();
		} else {
			Window.alert("You must select a Study period");
		}
	}

	@UiHandler("removeCourseBt")
	void onRemoveCourseBtClick(ClickEvent e){
		if(coursesLb.getSelectedIndex() != -1){
			studiesLb.getSelectedEntity().getCourses().remove(coursesLb.getSelectedEntity());
			coursesLb.removeSelectedEntity();
		}
	}
	@UiHandler("addSpBt")
	void onAddSpBtClick(ClickEvent e){
		dialogBoxSp = new DialogBox(false,true);
	    dialogBoxSp.setGlassEnabled(true);
	    dialogBoxSp.setAnimationEnabled(true);
	    dialogBoxSp.setText("Add a study period");
	    dialogBoxSp.setWidget(buildAddSpForm());
	    dialogBoxSp.center();
	    dialogBoxSp.show();
	}

	@UiHandler("removeSpBt")
	void onRemoveSpBtClick(ClickEvent e){
		if(studiesLb.getSelectedIndex() != -1){
			listOfStudies.remove(studiesLb.getSelectedIndex());
			studiesLb.removeItem(studiesLb.getSelectedIndex());
		}
	}

	@UiHandler("addEventBt")
	void onAddEventBtClick(ClickEvent e){
		if(coursesLb.getSelectedIndex() != -1){
			dialogBoxEvent = new DialogBox(false,true);
		    dialogBoxEvent.setGlassEnabled(true);
		    dialogBoxEvent.setAnimationEnabled(true);
		    dialogBoxEvent.setText("Add a event");
		    dialogBoxEvent.setWidget(buildAddEventForm());
		    dialogBoxEvent.center();
		    dialogBoxEvent.show();
		} else {
			Window.alert("You must select a course");
		}
	}

	@UiHandler("removeEventBt")
	void onRemoveEventBtClick(ClickEvent e){
		if(eventsLb.getSelectedIndex() != -1){
			eventsLb.removeItem(eventsLb.getSelectedIndex());
			coursesLb.getSelectedEntity().getEvents().remove(eventsLb.getSelectedEntity());
		}
	}

	private ClickHandler popupCourseSubmitHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent arg0) {
			final Course course = new Course(nameTb.getText(),typeTb.getText(),dbStart.getValue(),dbEnd.getValue(),studiesLb.getSelectedEntity());
			course.setEvents(new HashSet<Event>());
			studiesLb.getSelectedEntity().addCourse(course);
			coursesLb.addEntity(course);
		}
	};
	private ClickHandler popupCourseCloseHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent arg0) {
			dialogBoxCourse.hide();			
		}
	};
	private ClickHandler popupSpSubmitHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent arg0) {
			final StudyPeriod sp = new StudyPeriod(nameSpTb.getText(),typeSpTb.getStudyPeriodTypeSelected().getText(), null);
			sp.setCourses(new HashSet<Course>());
			listOfStudies.add(sp);
			studiesLb.addEntity(sp);
		}
	};
	private ClickHandler popupSpCloseHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent arg0) {
			dialogBoxSp.hide();			
		}
	};

	private ClickHandler popupEventSubmitHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent arg0) {
			final Event e = new Event(true,labelEventTb.getValue(),"to define",EventType.COURSE,null,null,Integer.parseInt(durationEventTb.getValue()),null,true);
			coursesLb.getSelectedEntity().addEvent(e);
			eventsLb.addEntity(e);
		}
	};
	private ClickHandler popupEventCloseHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent arg0) {
			dialogBoxEvent.hide();			
		}
	};
	
	//---------------------------------------------------------------------
	//-------------------------- UI action
	
	private Widget buildAddCourseForm() {
		nameTb = new TextBox();
		typeTb = new TextBox();
		dbStart = new DateBox();
		dbEnd = new DateBox();
		Grid grid = new Grid(5,2);
		grid.setWidget(0, 0, new Label("Name :"));
		grid.setWidget(0, 1, nameTb);
		grid.setWidget(1, 0, new Label("Type :"));
		grid.setWidget(1, 1, typeTb);
		grid.setWidget(2, 0, new Label("Date start :"));
		grid.setWidget(2, 1, dbStart);
		grid.setWidget(3, 0, new Label("Date end :"));
		grid.setWidget(3, 1, dbEnd);
		grid.setWidget(4, 0, popupCourseCloseBt);
		grid.setWidget(4, 1, popupCourseSubmitBt);
		return grid;
	}

	private Widget buildAddSpForm() {
		nameSpTb = new TextBox();
		typeSpTb = new StudyPeriodTypeListBox();
		Grid grid = new Grid(3,2);
		grid.setWidget(0, 0, new Label("Name :"));
		grid.setWidget(0, 1, nameSpTb);
		grid.setWidget(1, 0, new Label("Type :"));
		grid.setWidget(1, 1, typeSpTb);
		grid.setWidget(2, 0, popupSpCloseBt);
		grid.setWidget(2, 1, popupSpSubmitBt);
		return grid;
	}

	private Widget buildAddEventForm() {
		labelEventTb = new TextBox();
		durationEventTb = new TextBox();
		Grid grid = new Grid(5,2);
		grid.setWidget(0, 0, new Label("Label :"));
		grid.setWidget(0, 1, labelEventTb);
		grid.setWidget(1, 0, new Label("Type :"));
		grid.setWidget(2, 0, new Label("Duration :"));
		grid.setWidget(2, 1, durationEventTb);
		grid.setWidget(3, 0, new Label("Mandatory :"));
		grid.setWidget(4, 0, popupEventCloseBt);
		grid.setWidget(4, 1, popupEventSubmitBt);
		return grid;
	}
	public List<StudyPeriod> getListOfStudies() {
		return listOfStudies;
	}
}
